<template>
	<view class="info">
		<view class="userinfo">
			<button class="cu-btn" open-type="chooseAvatar" @chooseavatar="onChooseAvatar">
				<image :src="userInfo.avatar || '/static/user/avatar.png'" mode="aspectFill"></image>
			</button>

			<text>更换头像</text>
		</view>
		<view class="enum">
			<view class="cell">
				<view class="label">昵称</view>
				<view class="value" @click="handUpdName">
					<view class="name">
						{{ userInfo.nickName }}
					</view>
					<view class="icon">
						<uv-icon name="arrow-right"></uv-icon>
					</view>
				</view>
			</view>
			<view class="cell">
				<view class="label">用户ID</view>
				<view class="value">
					<view class="name">
						{{ userInfo.id }}
					</view>
				</view>
			</view>
			<view class="cell">
				<view class="label">手机号</view>
				<view class="value">
					<view class="name">
						{{ userInfo.cellphone }}
					</view>
				</view>
			</view>
		</view>

		<view class="enum" @click="handJump('/pagesA/bankCard/index')">
			<view class="cell">
				<view class="label">银行卡</view>
				<view class="value">
					<view class="icon">
						<uv-icon name="arrow-right"></uv-icon>
					</view>
				</view>
			</view>
		</view>

		<view class="enum">
			<view class="cell" @click="handJump('/pagesA/other/rich?val=yszc')">
				<view class="label">隐私政策</view>
				<view class="value">
					<view class="icon">
						<uv-icon name="arrow-right"></uv-icon>
					</view>
				</view>
			</view>
			<view class="cell" @click="handJump('/pagesA/other/rich?val=yhxy')">
				<view class="label">用户协议</view>
				<view class="value">
					<view class="icon">
						<uv-icon name="arrow-right"></uv-icon>
					</view>
				</view>
			</view>
			<view class="cell" @click="handJump('/pagesA/other/rich?val=rzxy')">
				<view class="label">入驻协议</view>
				<view class="value">
					<view class="icon">
						<uv-icon name="arrow-right"></uv-icon>
					</view>
				</view>
			</view>
		</view>

		<view class="action-bottom">
			<view class="btn" @click="signOut">
				<text>退出登录</text>
			</view>
		</view>
		<upd-name ref="updRef" @updChange="requestUpd" />
	</view>
</template>

<script setup>
	import {
		ref,
		toRaw
	} from 'vue'
	import {
		useAppsStore
	} from '@/stores/modules/app.js'
	import {
		storeToRefs
	} from 'pinia'
	import {
		doUpdInfo
	} from '@/apis/user.js'
	import uploadFile from '@/utils/ossutil/uploadFile.js'
	import UpdName from './components/updName.vue'

	const useStore = useAppsStore()
	const {
		doUserInfo,
		signOut
	} = useStore
	const {
		getUserInfo: userInfo
	} = storeToRefs(useStore)


	const onChooseAvatar = (e) => {
		uploadFile(e.detail.avatarUrl, 'images/', e.detail.avatarUrl, (avatar => {
			requestUpd({
				avatar
			})
		}), (err => {

		}), (res => {

		}))
	}

	const updRef = ref(null)
	const handUpdName = () => {
		updRef.value.handOpen(toRaw(userInfo.value).nickName)
	}

	const requestUpd = async (params) => {
		await doUpdInfo({
			id: userInfo.value.id,
			...params
		})
		doUserInfo()
	}

	const handJump = (url) => {
		uni.navigateTo({
			url
		})
	}
</script>

<style lang="scss" scoped>
	.info {
		min-height: 100vh;
		background: #F5F5F5;

		.userinfo {
			background-color: #fff;
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;
			padding: 44rpx;

			.cu-btn {
				height: 150rpx;
				background-color: rgba(255, 255, 255, 0);
			}

			image {
				width: 150rpx;
				height: 150rpx;
				border-radius: 50%;
			}

			text {
				margin-top: 24rpx;
				font-size: 32rpx;
				font-family: PingFang SC-Bold, PingFang SC;
				font-weight: bold;
				color: #06121E;
			}
		}

		.enum {
			margin-top: 16rpx;
			padding: 0 24rpx;
			background-color: #fff;

			.cell {
				display: flex;
				align-items: center;
				justify-content: space-between;
				padding: 20rpx 0;

				.label {
					font-size: 30rpx;
					font-family: PingFang SC-Medium, PingFang SC;
					font-weight: 500;
					color: #333333;
				}

				.value {
					flex: 1;
					margin-left: 20rpx;
					display: flex;
					align-items: center;
					justify-content: flex-end;

					.name {
						max-width: 450rpx;
						font-size: 30rpx;
						font-family: PingFang SC-Bold, PingFang SC;
						color: #333333;
					}

					.avatar {
						width: 96rpx;
						height: 96rpx;
						border-radius: 50%;
					}

					.icon {
						margin-left: 6rpx;
					}
				}
			}
		}

		.action-bottom {
			position: fixed;
			bottom: 0;
			left: 0;
			width: 750rpx;
			padding: 20rpx 0 80rpx 0;

			.btn {
				width: 654rpx;
				height: 84rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				background-color: rgba(255, 255, 255, 0);
				margin: 0 auto;
				border: 1rpx solid #FF930D;
				border-radius: 16rpx;
			}

			text {
				margin-left: 20rpx;
				font-size: 30rpx;
				font-family: PingFang SC-Regular, PingFang SC;
				font-weight: 400;
				color: #F57B01;
			}
		}
	}
</style>